const CommomUtil = require('../utils/CommomUtil');
const SessionUtil = require('../utils/SessionUtil');

const albums_dao = require('../dao/albums_dao');
const comments_dao = require('../dao/comments_dao');
const redis_session = require('../config').redis_user;

/** 发表评论 */
module.exports.pub_comments = pub_comments;
async function pub_comments(ctx) {
    let { ty_id, ty_s_id, item_id, comment_content, item_type } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(item_id, 32) || CommomUtil.inValid(comment_content, 1000)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(item_type)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let [flag, msg] = await comments_dao.publish_comments(ty_id, item_id, comment_content, item_type, null, null, null, null);
    resData['ret'] = flag;
    flag ? (resData['msg'] = '发布成功!', resData['comment_id'] = msg) : resData['msg'] = msg;
    return ctx.body = resData;
}

/** 删除评论 */
module.exports.del_comments = del_comments;
async function del_comments(ctx) {
    let { ty_id, ty_s_id, comment_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(comment_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(comment_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [del_flag, msg] = await comments_dao.del_comments(comment_id, ty_id);
    resData['ret'] = del_flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 发表回复 */
module.exports.reply_comments = reply_comments;
async function reply_comments(ctx) {
    let { ty_id, ty_s_id, by_reply_id, by_replyer_id, item_id, comment_content } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(by_reply_id) || CommomUtil.inValid(by_replyer_id, 32) || CommomUtil.inValid(item_id, 32) || CommomUtil.inValid(comment_content, 1000)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(by_reply_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let [by_reviewer_id, by_reviewer_name, by_reviewer_content, item_type] = await comments_dao.get_by_reply_content(by_replyer_id, by_reply_id);
    if (by_reviewer_id == null || by_reviewer_name == null) {
        resData['ret'] = false;
        resData['msg'] = '非法请求!';
        return ctx.body = resData;
    }
    let [flag, msg] = await comments_dao.publish_comments(ty_id, item_id, comment_content, item_type, by_reply_id, by_reviewer_content, by_reviewer_name, by_reviewer_id);
    resData['ret'] = flag;
    flag ? (resData['msg'] = '发布成功!', resData['comment_id'] = msg) : resData['msg'] = msg;
    return ctx.body = resData;
}


/** 评论 点赞/取消点赞  */
module.exports.update_like_num = update_like_num;
async function update_like_num(ctx) {
    let { ty_id, ty_s_id, comment_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(comment_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(comment_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }

    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let like_num = await comments_dao.get_comments_like_num(comment_id);
    if (like_num == null) {
        resData['ret'] = false;
        resData['msg'] = '非法操作!';
        return ctx.body = resData;
    }
    let key = ty_id + '_like_' + comment_id;
    let str = await redis_session.get(key);
    let like_status = true;
    if (CommomUtil.isEmpty(str)) {
        if (await redis_session.set(key, 1) == 'OK') {
            let flag = await comments_dao.update_like_num(comment_id, 1);
            if (flag) like_num += 1;
            like_status = true;
        }
    } else {
        if (await redis_session.del(key)) {
            let flag = await comments_dao.update_like_num(comment_id, -1);
            if (flag) like_num -= 1;
            like_status = false;
        }
    }
    resData['like_num'] = like_num;
    resData['like_status'] = like_status;
    resData['ret'] = true;
    resData['msg'] = '操作成功!';
    return ctx.body = resData;
}

/** 获取评论列表  */
module.exports.query_comments_list = query_comments_list;
async function query_comments_list(ctx) {
    let { ty_id, ty_s_id, item_id, begin, plen } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(item_id, 32) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || CommomUtil.inValidNumber(plen, 50)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }

    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let hot_comments = [];
    // if (begin == 0) {
    //     hot_comments = await comments_dao.get_hot_comments_list(item_id);
    // }
    let comments = await comments_dao.get_comments_list(item_id, begin, plen);
    for (let comment of comments) {
        hot_comments.push(comment);
    }
    /** 异步循环 */
    let comment_list = [];
    for (let comment of hot_comments) {
        /** 设置是否点赞 */
        comment_list.push(get_like_status(comment, ty_id));
    }
    let comments_list = [];
    await Promise.all(comment_list).then((resultLists) => {
        for (let item of resultLists) {
            comments_list.push(item);
        }
    });
    resData['comments'] = comments_list;
    resData['ret'] = true;
    resData['msg'] = '操作成功!';
    return ctx.body = resData;
}

/** 查询是否点赞 */
async function get_like_status(comment, ty_id) {
    let key = ty_id + '_like_' + comment['comment_id'];
    let str = await redis_session.get(key);
    if (!CommomUtil.isEmpty(str)) {
        comment['like_status'] = true;
    } else {
        comment['like_status'] = false;
    }
    return comment;
}